import os
import pickle

SETTING_FILE_NAME = 'ddu_settings.dat'
class Setting():
    _initialized = 0
    items = {'username':('t', ''),
             'password':('t', ''),
             'save_password':('f', 0)
             }

    @classmethod
    def init(cls):
        if cls._initialized == 0:
            #load data from database
            if os.path.exists(SETTING_FILE_NAME):
                cls.load()
            else:
                cls.create()
            cls._initialized = 1
        else:
            return

    @classmethod
    def create(cls):
        cls.data = {}
        for i in cls.items.keys():
            cls.data[i] = cls.items[i][1]
        #cls.save()

    @classmethod
    def save(cls):
        f = open(SETTING_FILE_NAME, 'w+')
        pickle.dump(cls.data, f)
        f.close()

    @classmethod
    def load(cls):
        f = open(SETTING_FILE_NAME, 'r')
        cls.data = pickle.load(f)
        f.close()

    @classmethod
    def get(cls, k):
        return cls.data[k]

    @classmethod
    def set(cls, k, v):
        cls.data[k] = v

